package model;

import enums.URLEnum;
import misc.BundlesProvider;

import java.util.ResourceBundle;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Analiza el sexo de los terminos del silogismo;
 *
 * @author Jose Martin Rozanec;
 */
public class LexicAnalizer {
    /**
     * Analiza si el termino corresponde al sexo femenino;
     *
     * @param article;
     * @param word;
     * @return boolean;
     */
    public static boolean analizeFemenino(String article, String word) {
        String temp;
        ResourceBundle bundle = ResourceBundle.getBundle("femeninos");
        //sustantivos con significado varia dependiendo del genero o puede ser
        // aceptado de forma indistinta para ambos.
        String[] exceptions = bundle.getString("excepcionesFemenino").split(",");
        for (int j = 0; j < exceptions.length; j++) {
            if (exceptions[j].equalsIgnoreCase(word)) {
                return false;
            }
        }
        String[] sustantivosArray = bundle.getString("significadoVariable").split(",");
        temp = word.toLowerCase();
        if (article != null) {
            for (String aSustantivosArray : sustantivosArray) {
                if (temp.equalsIgnoreCase(aSustantivosArray)) {
                    String aux = article.toLowerCase();
                    return aux.contains("toda") || aux.contains("alguna") || aux.contains("ninguna");
                }
            }
        }

        if (temp.endsWith("dad") || temp.endsWith("tad") || temp.endsWith("ie") || temp.endsWith("ion") ||
                temp.endsWith("sis") || temp.endsWith("ez") || temp.endsWith("triz") ||
                temp.endsWith("dad ") || temp.endsWith("tad ") || temp.endsWith("ie ") ||
                temp.endsWith("ion ") || temp.endsWith("sis ") ||
                temp.endsWith("ez ") || temp.endsWith("triz ")) {
            return true;
        }

        //verifica bundle y episcenos; completar lista
        String[] heteronimosArray = bundle.getString("heteronimos").split(",");
        for (String aHeteronimosArray : heteronimosArray) {
            if (temp.equalsIgnoreCase(aHeteronimosArray)) {
                return true;
            }
        }
        if (article == null) {
            temp = word.toLowerCase();
            if (temp.endsWith("a") || temp.endsWith("as") || temp.endsWith("a ") || temp.endsWith("as ")) {
                return true;
            }
        } else {
            temp = article.toLowerCase();
            if (temp.contains("toda") || temp.contains("alguna") || temp.contains("ninguna")) {
                return true;
            }
        }
        return false;
    }

    /**
     * Analiza si el significado varia con el sexo;
     *
     * @param word;
     * @return boolean;
     */
    public static boolean analizeSignificadoVariaConSexo(String word) {
        return matches(word, "significadoVariable");
    }

    /**
     * Analiza si el sexo no se convierte variando a/o;
     *
     * @param word;
     * @return boolean;
     */
    public static boolean analizeSexoInconvertible(String word) {
        return matches(word, "sexoInconvertible");
    }

    /**
     * Revisa si el termino matchea con los terminos femeninos especificados en el ResourceBundle;
     *
     * @param word;
     * @param bundleString;
     * @return boolean;
     */
    private static boolean matches(String word, String bundleString) {
        ResourceBundle heteronimos = ResourceBundle.getBundle("femeninos");
        String[] sustantivosArray = heteronimos.getString(bundleString).split(",");
        word = word.toLowerCase();
        for (int j = 0; j < sustantivosArray.length; j++) {
            if (sustantivosArray[j].equals(word)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Usado para convertir una palabra de sexo masculino a sexo femenino;
     *
     * @param word;
     * @return String;
     */
    public static String convertToFemenino(String word) {
        if (word.endsWith("o") && !heteronimoMasculino(word)) {
            return word.substring(0, word.length() - 1) + "a";
        }
        return word;
    }

    /**
     * Elmetodo verifica si la palabra que se le pasa es un heteronimo;
     *
     * @param word;
     * @return String;
     */
    public static boolean heteronimoMasculino(String word) {
        ResourceBundle heteronimos = ResourceBundle.getBundle("femeninos");
        String[] sustantivosArray = heteronimos.getString("heteronimoMasculino").split(",");
        word = word.toLowerCase();
        for (int j = 0; j < sustantivosArray.length; j++) {
            if (sustantivosArray[j].equals(word)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Analiza si la palabra pasada se halla en plural;
     *
     * @param word;
     * @return String;
     */
    public static boolean analizePlural(String word) {
        String temp = word.toLowerCase();
//        &&(!temp.endsWith(" s"))
        if (((temp.endsWith("s") || temp.endsWith("s "))) && temp.length() > 1 &&!analizeExceptions(word)) {
            return true;
        }
        return false;
    }

    /**
     * Returns true if the word is detected as an exception;
     * @param word;
     * @return boolean;
     */
    public static boolean analizeExceptions(String word){
        ResourceBundle bundle = BundlesProvider.getBundle(URLEnum.PLURAL_EXCEPTIONS_BUNDLE);
        String string = bundle.getString("excepciones");
        String [] exceptions = string.split(",");
        for(int j=0;j<exceptions.length;j++){
            if(exceptions.equals(word)){
                return true;
            }
        }
        return false;
    }
}
